﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;


namespace CubicFWNet.Data.SystemManager
{
    public class CustomerProductRepository : RepositoryBase<CustomerProduct>
    {
        /// <summary>
        /// 获取产品对应客户列表
        /// </summary>
        /// <param name="cusID"></param>
        /// <returns></returns>
        public IQueryable<ProductView> GetCusProductList(Guid cusID)
        {
            var result = from pro in dbcontext.Products
                         join cusPro in dbcontext.CustomerProducts on pro.Id equals cusPro.ProductID
                         where cusPro.CustomerID == cusID
                         select new ProductView
                         {
                             Id = pro.Id,
                             Name = pro.Name,
                             Number = pro.Number,
                             //Type = pro.Type,
                             Price = pro.Price,

                             CustomerID = cusPro.CustomerID,
                             ProductID = cusPro.ProductID,
                             ProductByName = cusPro.ProductByName,
                             PriceCustomer = cusPro.Price,
                             Amount = 0
                         };
            return result;
        }

        /// <summary>
        /// 查询客户已关联产品
        /// </summary>
        /// <param name="cusID"></param>
        /// <returns></returns>
        public IQueryable<ProductView> GetCustomerProList(Guid cusID)
        {
            var result = from cusPro in dbcontext.CustomerProducts
                         join pro in dbcontext.Products on cusPro.ProductID equals pro.Id
                         where cusPro.CustomerID == cusID
                         select new ProductView
                         {
                             Id = pro.Id,
                             Name = pro.Name,
                             Number = pro.Number,
                             //Type = pro.Type,
                             Price = pro.Price,

                             CustomerID = cusPro.CustomerID,
                             ProductID = cusPro.ProductID,
                             ProductByName = cusPro.ProductByName,
                             PriceCustomer = cusPro.Price,
                             Amount = 0,

                         };
            return result;
        }

        /// <summary>
        /// 查询客户购买过的所有产品
        /// </summary>
        /// <param name="cusID"></param>
        /// <returns></returns>
        public IQueryable<ProductView> GetCustomerOrderProductList(Guid cusID)
        {
            var result = from op in dbcontext.OrderProducts
                         join pro in dbcontext.Products on op.ProductId equals pro.Id
                         join order in dbcontext.Orders on op.OrderId equals order.Id
                         join cusPro in dbcontext.CustomerProducts on new { id1 = cusID, id2=op.ProductId.Value }  equals new { id1=cusPro.CustomerID,id2=cusPro.ProductID} 
                         where order.CustomeId == cusID && pro.Status==1 && op.FinalPrice>0
                         select new ProductView
                         {
                             Id = pro.Id,
                             Name = pro.Name,
                             Number = pro.Number,
                             //Type = pro.Type,
                             Price = pro.Price,
                             FinalPrice = op.FinalPrice,
                             CostPrice=op.CostPrice,
                             CustomerID = order.CustomeId.Value,
                             ProductID = op.ProductId.Value,
                             ProductByName = cusPro.ProductByName,
                             PriceCustomer = cusPro.Price,
                             Amount = 0,
                             PriceTime = op.PriceTime,
                             OrderProductID = op.Id,
                         };
            return result;
        }
    }
}
